//
//  TimerViewController.m
//  Timer
//
//  Created by Ashish Tarhalkar on 6/4/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TimerViewController.h"
#import "SettingsViewController.h"
#import "Settings.h"

@implementation TimerViewController

-(void) ScheduleAlarmAfterSeconds:(int)seconds {

	if (NSClassFromString(@"UILocalNotification")) {

		UILocalNotification *alarm = [[[UILocalNotification alloc] init] autorelease];

		if (alarm) {
			
			alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:seconds];
			alarm.timeZone = [NSTimeZone defaultTimeZone];
			alarm.repeatInterval = 0;
			alarm.alertBody = @"Time is up!!!";
			
			[[UIApplication sharedApplication] scheduleLocalNotification:alarm];
		}
	}
	else {
		// schedule timers
		[self performSelector:@selector(StopTimer) withObject:nil afterDelay:seconds];
	}
}

-(void) CancelAlarms {

	if (NSClassFromString(@"UILocalNotification")) {

		UIApplication *app = [UIApplication sharedApplication];
		NSArray *oldNotifications = [app scheduledLocalNotifications];
		if ([oldNotifications count] > 0) {
			[app cancelAllLocalNotifications];
		}
	}
	else {

		// unschedule timers
		[NSTimer cancelPreviousPerformRequestsWithTarget:self selector:@selector(StopTimer) object:nil];
	}
}


-(IBAction) StartTimer {

	currentTimer = 1;
	timer = [NSTimer scheduledTimerWithTimeInterval:settings.timer1 target:self selector:@selector(timerElapsed:) userInfo:nil repeats:YES];

	buttonStart.enabled = NO;
	buttonEnd.enabled = YES;
	[[UIApplication sharedApplication] setIdleTimerDisabled:YES]; //disable autolocking
	[[UIDevice currentDevice] setProximityMonitoringEnabled:YES]; //enable screen dimming	
}


-(IBAction) EndTimer {

	[timer invalidate];
	[self CancelAlarms];
	buttonStart.enabled = YES;
	buttonEnd.enabled = NO;	
}

-(IBAction) OpenSettings {

	SettingsViewController* settingsController = [[SettingsViewController alloc]initWithNibName:@"SettingsViewController" bundle:nil];
	settingsController.settings = settings;
	settingsController.persister = persister;

	[self presentModalViewController:settingsController animated:YES];
}

-(void) timerElapsed: (NSTimer*) theTimer {
	
	[timer invalidate];

	int timerSelected;
	if (currentTimer == 1) {
		currentTimer = 2;
		timerSelected = settings.timer2;
	}
	else {
		currentTimer = 1;
		timerSelected = settings.timer1;
	}

	[self ScheduleAlarmAfterSeconds:1];
	timer = [NSTimer scheduledTimerWithTimeInterval:timerSelected target:self selector:@selector(timerElapsed:) userInfo:nil repeats:YES];	
}


-(void) StopTimer {

	[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
	[self ShowMessage];
}


-(void) ShowMessage:(NSString*) message {

	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:message message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
	[alert show];
	[alert release];
}


-(void) ShowMessage {

	[self ShowMessage:@"Time is up!!!"];
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}


-(void)viewDidLoad {

	settings = [[Settings alloc]init];

	persister = [NSUserDefaults standardUserDefaults];
	NSInteger timerSetting = [persister integerForKey:@"timer1"];

	if (timerSetting != 0) {
		settings.timer1 = timerSetting;
	}
	else {
		settings.timer1 = 5;
		[persister setInteger:settings.timer1 forKey:@"timer1"];
		[persister synchronize];
	}
	
	timerSetting = [persister integerForKey:@"timer2"];
	
	if (timerSetting != 0) {
		settings.timer2 = timerSetting;
	}
	else {
		settings.timer2 = 10;
		[persister setInteger:settings.timer2 forKey:@"timer2"];
		[persister synchronize];
	}
}

- (void)viewDidUnload {
	
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end
